/**
 * DownLoadUtil.java
 * <p>
 * 代码相关：
 * ExcelUtil.java
 * ResultSetWrap.java
 * StringUtil.java
 * 
 * </P>
 * Description: 下载相关
 * Created By Swiftly
 * Created On Nov 9, 2009
 * Version: v1.0.0 
 * CopyRight: centurial-tech 
 *
 **/

package com.ys.malini.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownLoadUtil {

	/**
	 * 下载文件
	 * 
	 * @param response
	 * @param fileName
	 * @throws Exception
	 */
	public static void downloadFile(HttpServletResponse response,
			String fileName) throws Exception {
		FileInputStream instream = null;
		OutputStream outstream = null;
		File finalOutFile = null;

		try {
			finalOutFile = new File(fileName);
			instream = new FileInputStream(finalOutFile);
			outstream = response.getOutputStream();
			response.setContentType("application/octet-stream");
			response.setHeader("Content-disposition", "attachment; filename="
					+ java.net.URLEncoder.encode(finalOutFile.getName(),
							"UTF-8"));
			response.setContentLength((int) finalOutFile.length());

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = instream.read(buffer)) > 0) {
				outstream.write(buffer, 0, len);
			}
			outstream.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("下载文件异常" + e.getMessage());
		} finally {
			try {
				if (instream != null) {
					instream.close();
					instream = null;
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}

			try {
				if (outstream != null) {
					outstream.close();
					outstream = null;
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

	/**
	 * 根据查询SQL语句下载Excel文件
	 * 
	 * 
	 * @param jdbcUtil
	 *            数据库链接
	 * @param querySQL
	 *            查询SQL包括中文描述 select name as 姓名 from tb_user
	 * @param fileName
	 *            下载文件名
	 * @param sheetName
	 *            excel表名
	 * @param response
	 */
	// public static void downloadExcelFile(JdbcUtil jdbcUtil, String querySQL,
	// String fileName, String sheetName, HttpServletResponse response)
	// throws Exception {
	// OutputStream outstream = null;
	// ResultSetWrap rst = null;
	//
	// try {
	// if (jdbcUtil == null)
	// throw new Exception("数据库连接为空，无法查询数据");
	//
	// if (StringUtils.isEmpty(querySQL))
	// throw new Exception("查询语句为空，无法查询数据");
	//
	// if (StringUtils.isEmpty(fileName))
	// fileName = DateUtil.longDate(DateUtil.getSysDate());
	//
	// if (StringUtils.isEmpty(sheetName))
	// sheetName = "sheet0";
	//
	// outstream = response.getOutputStream();
	//
	// response.reset();
	//
	// response.setContentType("application/msexcel");
	// response.setHeader("Content-disposition", "attachment; filename="
	// + java.net.URLEncoder.encode(fileName, "UTF-8"));
	//
	// rst = jdbcUtil.excuteQuery(querySQL);
	//
	// ExcelUtil.createExcelFromResultWrap(rst, outstream, sheetName);
	//
	// outstream.flush();
	// } catch (Exception e) {
	// e.printStackTrace();
	// throw new Exception("下载文件异常" + e.getMessage());
	// } finally {
	//
	// try {
	// if (outstream != null) {
	// outstream.close();
	// outstream = null;
	// }
	// } catch (IOException ioe) {
	// ioe.printStackTrace();
	// }
	//
	// DaoBase.releaseResultSetWrap(rst);
	// }
	// }
	/**
	 * 以response下载指定的List数据成Excel
	 * 
	 * @param fileName
	 *            文件名
	 * @param sheetName
	 *            表名
	 * @param rst
	 *            下载的结果集
	 * @param methods
	 *            结果集对像的方法
	 * @param cellTitle
	 *            表头描述
	 * @param response
	 * @throws Exception
	 */
	public static void downloadExcelFile2(String fileName, String sheetName,
			List rst, String[] methods, String[] cellTitle,
			HttpServletResponse response, HttpServletRequest req)
			throws Exception {
		downloadExcelFromListWithOutAndWidth(fileName, sheetName, rst, methods,
				cellTitle, null, response, req);

	}

	/**
	 * 以response下载指定的List数据成Excel2
	 * 
	 * @param fileName
	 * @param sheetName
	 * @param rst
	 * @param methods
	 * @param cellTitle
	 * @param cellWidth
	 * @param response
	 * @throws Exception
	 */
	public static void downloadExcelFromListWithOutAndWidth(String fileName,
			String sheetName, List rst, String[] methods, String[] cellTitle,
			int[] cellWidth, HttpServletResponse response,
			HttpServletRequest req) throws Exception {
		OutputStream outstream = null;

		try {
			if (StringUtils.isEmpty(fileName))
				fileName = DateUtil.longDate(DateUtil.getSysDate());

			if (StringUtils.isEmpty(sheetName))
				sheetName = "sheet0";

			outstream = response.getOutputStream();

			response.reset();

			response.setContentType("application/msexcel");
			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");

			// Get IE Version
			boolean IEVersion6_0 = (req.getHeader("User-Agent").indexOf(
					"MSIE 6.0") > 0);
			
			if (IEVersion6_0) {
				response.setHeader("Content-Disposition", "filename="
						+ fileName);
			}else{
				response.setHeader("Content-Disposition", "attachment;filename="
						+ fileName);
			}

			ExcelUtil.createExcelFromBeanList(fileName, sheetName, rst,
					methods, cellTitle, cellWidth, outstream);

			outstream.flush();
		

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("下载文件异常" + e.getMessage());
		} finally {

			try {
				if (outstream != null) {
					outstream.close();
					outstream = null;
				}
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}
}
